<template>
  <el-form :model="formData" size="small" :rules="rules" ref="formRef" label-width="84px">
    <el-form-item label="账号" prop="username">
      <el-input v-model="formData.username" :maxlength="32"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="formData.nickname" :maxlength="32"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="formData.password" :maxlength="32" type="password"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="formData.phone" :maxlength="11"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <yyUpload v-model="formData.avatar" />
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-radio-group v-model="formData.sex">
        <el-radio v-for="item in sexOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-radio-group v-model="formData.state">
        <el-radio v-for="item in stateOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleConfirm">保存</el-button>
      <el-button @click="handleReset">重置</el-button>
      <el-button @click="handleClose">关闭</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { sexOptions, stateOptions } from "@/constant/defaultOptions.js";
import { validatorPhone } from "@/constant/defaultValidator.js";
import crypto from "@/utils/crypto.js";
import { createLoading, createMessage } from "@/utils/index.js";
import { goBack } from "@/utils/browser.js";
import yyUpload from "@/components/yyUpload/index.vue";
export default {
  name: "system_user_add",
  components: { yyUpload },
  data() {
    return {
      sexOptions,
      stateOptions,
      formData: {
        nickname: "",
        username: "",
        sex: "",
        state: "",
        password: "",
        phone: "",
        avatar: "",
      },
      rules: {
        nickname: [{ required: true, message: "必填", trigger: "blur" }],
        username: [{ required: true, message: "必填", trigger: "blur" }],
        sex: [{ required: true, message: "必填", trigger: "change" }],
        state: [{ required: true, message: "必填", trigger: "change" }],
        password: [{ required: true, message: "必填", trigger: "blur" }],
        phone: [
          { required: true, message: "必填", trigger: "blur" },
          { validator: validatorPhone, trigger: "blur" },
        ],
        avatar: [{ required: true, message: "必填", trigger: "change" }],
      },
    };
  },
  methods: {
    handleConfirm() {
      this.$refs.formRef.validate(async (isOk) => {
        if (!isOk) {
          return createMessage("请完成必填项的填写与选择");
        }
        const params = {
          ...this.formData,
        };
        params.password = crypto.md5(params.password);
        const loading = createLoading("保存中，请稍等");
        try {
          await this.$globalApi.user.sysUserInsertApi(params);
          createMessage("保存成功", { type: "success" });
          this.$emit("refresh");
          goBack("/system/user");
        } catch (err) {
          console.log("新增用户报错了", err);
        } finally {
          loading.close();
        }
      });
    },
    handleReset() {
      this.$refs.formRef.resetFields();
    },
    handleClose() {
      goBack("/system/user");
    },
  },
};
</script>

<style></style>